package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.PageResponse;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author Chen_Feng
 * @since 2022/07/09 08:54
 */
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    /**
     * 创建商品订单，创建成功则返回订单id，创建失败则返回错误异常信息
     *
     * @param userInfo           userInfo请求头
     * @param createOrderRequest 封装了请求体响应数据的对象
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author Chen_Feng
     * @since 2022/07/09 9:13
     */
    @PostMapping("order")
    public ResponseData create(@RequestHeader("user_info") String userInfo, @RequestBody CreateOrderRequest createOrderRequest) {

        // 获取userInfo中的userId,并赋值给request成员变量
        JSONObject object = JSON.parseObject(userInfo);
        Long userId = Long.parseLong(object.get("uid").toString());
        createOrderRequest.setUserId(userId);

        // 执行参数校验
        createOrderRequest.requestCheck();

        // 执行业务层
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(createOrderRequest);

        // 如果状态码为成功则返回结果，否则返回错误异常
        if (SysRetCodeConstants.SUCCESS.getCode().equals(createOrderResponse.getCode())) {
            return new ResponseUtil().setData(createOrderResponse.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(createOrderResponse.getMsg());
    }

    /**
     * 获取用户的订单列表
     *
     * @param userInfo         用户信息
     * @param orderListRequest 分页信息
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author Chen_Feng
     * @since 2022/07/09 20:49
     */
    @GetMapping("order")
    public ResponseData orderList(@RequestHeader("user_info") String userInfo, OrderListRequest orderListRequest) {

        // 获取userInfo中的userId,并赋值给request成员变量
        JSONObject object = JSON.parseObject(userInfo);
        Object uid = object.get("uid");
        if (uid == null) {
            return new ResponseUtil().setErrorMsg(SysRetCodeConstants.GET_TOKEN_FAILED.getMessage());
        }
        Long userId = Long.parseLong(uid.toString());
        orderListRequest.setUserId(userId);

        // 参数校验
        orderListRequest.requestCheck();

        // 执行业务层
        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);

        // 如果状态码为成功则返回结果，否则返回错误异常
        if (SysRetCodeConstants.SUCCESS.getCode().equals(orderListResponse.getCode())) {
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(orderListResponse.getDetailInfoList());
            pageResponse.setTotal(orderListResponse.getTotal());
            return new ResponseUtil().setData(pageResponse);
        }
        return new ResponseUtil().setErrorMsg(orderListResponse.getMsg());
    }

    /**
     * 获取指定订单的订单详情
     *
     * @param id 订单的id
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author Chen_Feng
     * @since 2022/07/09 20:49
     */
    @GetMapping("order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String id) {

        /*
        // 参数校验
        if (id == null || "".equals(id.trim())) {
            return new ResponseUtil().setErrorMsg(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        */

        // 创建一个request对象
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id);

        // 参数校验
        orderDetailRequest.requestCheck();

        // 执行
        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(orderDetailRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(orderDetailResponse.getCode())) {
            return new ResponseUtil().setData(orderDetailResponse.getOrderDetailDto());
        }
        return new ResponseUtil().setErrorMsg(orderDetailResponse.getMsg());
    }

    /**
     * 取消订单
     *
     * @param jsonStr 包含订单id的json字符串
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author Chen_Feng
     * @since 2022/07/09 22:38
     */
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody String jsonStr) {

        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String orderId = jsonObject.get("orderId").toString();

        /*
        // 校验参数
        if (orderId == null || "".equals(orderId.trim())) {
            return new ResponseUtil().setErrorMsg(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        */

        // 创建请求对象
        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(orderId);

        // 校验参数
        cancelOrderRequest.requestCheck();

        // 执行业务层
        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
        if (SysRetCodeConstants.SUCCESS.getCode().equals(cancelOrderResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(cancelOrderResponse.getMsg());
    }

    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {

        /*
        // 校验参数
        if (id == null || "".equals(id.trim())) {
            return new ResponseUtil().setErrorMsg(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        */

        // 创建请求对象
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);

        // 校验参数
        deleteOrderRequest.requestCheck();

        // 执行业务层
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(deleteOrderRequest);
        if (SysRetCodeConstants.SUCCESS.getCode().equals(deleteOrderResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(deleteOrderResponse.getMsg());
    }
}
